package ga.wavaplayer.ui.buttons {
	
	import flash.events.Event;
	import ga.wavaplayer.ui.events.WPlayerUIEvent;
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import ga.wavaplayer.ui.WControls;
	import ga.wemanager.WEManager;
	
	public class WIconButton extends WButton{
	
		public var normalColor:uint;
		public var hoverColor:uint;
		
		private var _mask:Bitmap;
		private var _eventType:String;
		
		private var _normalState:Sprite = new Sprite();
		private var _hoverState:Sprite = new Sprite();
		
		public function WIconButton(pConfig:Object, pMask:Bitmap, pNormalColor:uint = 0xcccccc, pHoverColor:uint = 0xffffff, pEventType:String = WControls.ICON_CLICKED) {
			
			//	Save Colors
			normalColor = pNormalColor;
			hoverColor = pHoverColor;
			
			_eventType = pEventType;
			
			//	Set mask
			_mask = pMask;
			_mask.cacheAsBitmap = true;
			
			super(pConfig);
			
			this.addEventListener(MouseEvent.MOUSE_OVER, _mouseOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, _mouseOutHandler);
			this.addEventListener(MouseEvent.CLICK, _clickHandler);
		}
		
		protected override function _setVisual():void {
			
			_normalState.graphics.beginFill(normalColor);
			_normalState.graphics.drawRect(0, 0, _config.height, _config.height);
			_normalState.graphics.endFill();
			
			_hoverState.graphics.beginFill(hoverColor);
			_hoverState.graphics.drawRect(0, 0, _config.height, _config.height);
			_hoverState.graphics.endFill();
			
			_visual.cacheAsBitmap = true;
			_visual.mask = _mask;
			_visual.addChild(_mask);
			
			_visual.addChild(_normalState);
			
		}
		
		private function _mouseOutHandler(e:MouseEvent):void {
			_visual.addChild(_normalState);
			if (_visual.contains(_hoverState)) {
				_visual.removeChild(_hoverState);
			}
		}
		
		private function _mouseOverHandler(e:MouseEvent):void {
			_visual.addChild(_hoverState);
			if (_visual.contains(_normalState)) {
				_visual.removeChild(_normalState);
			}
		}
		
		private function _clickHandler(e:MouseEvent):void {
			var event:Event = new Event(_eventType);
			WEManager.dispatchEvent(event);
		}
	}
}
